using System;

using Microsoft.SPOT;

using mfRCF.Core;
using mfRCF.Core.Extensions;

using mfXmlSerializer;

using mfLogger;

using OST.Shared.SharedTypes;
using OST.Persistence;
using OST.Shared;

namespace OST.Commands
{
    using Extensions;
    using OST.Motor;
    using OST.Calc.Frame;

    /// <summary>
    /// drives to motor-alpha=0 (frame-center-position)
    /// </summary>
    public class MotorDrive2CenterGETCommand : mfRcfCommand
    {
        #region CTOR

        public MotorDrive2CenterGETCommand(string httpPath)
            : base(httpPath, new mfGETMethod())
        {
        }

        #endregion

        #region Properties

        /// <summary>
        /// wrapper-property 4 logging
        /// </summary>
        private ILoggerAppender Logger
        {
            get
            {
                return mfLogger.mfLogger.Instance;
            }
        }

        #endregion

        #region mfRcfCommand
        public override void ExecuteCommand(mfIHttpContext httpContext)
        {
            try
            {
                if (!MotorProvider.Instance.HasInstance)
                    throw new OSTException("no motor instance found");

                if (!MotorProvider.Instance.MotorInstance.IsInitialized)
                    throw new OSTException("motor not initialized");

                if (!ConfigurationProvider.Instance.HasConfig)
                    throw new OSTException("no frame-config available");

                var frameConfig = ConfigurationProvider.Instance.ConfigInstance.Frame;

                //calc motor-zero
                var center = FrameCalculator.CalcMotorStrokeZero(frameConfig.Arm
                                                                    , frameConfig.Leg
                                                                    , frameConfig.Body);

                MotorProvider.Instance.MotorInstance.Move2MotorPositionAsync(center);

                var msg = "motorZero=" + center + "mm ";

                this.WriteHttpResponseOK(httpContext);
            }
            catch (Exception ex)
            {
                this.WriteHttpResponseError(httpContext, ex);
            }
        }
        #endregion
    }
}
